package com.ddstation.clinic.dpt.service;

import com.ddstation.clinic.dpt.entity.DtClinicDepartmentEntity;
import com.ddstation.clinic.dpt.mapper.DtClinicDepartmentMapper;
import org.jdom.IllegalDataException;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.List;

@Service @Lazy
public class ClinicDptServiceImpl implements ClinicDptService {

	@Override
	@Transactional(readOnly=true)
	public DtClinicDepartmentEntity get(String institutionId, String dptCd) {
		// TODO Auto-generated method stub
		return dtClinicDepartmentMapper.selectByPK(institutionId, dptCd);
	}

	@Override
	@Transactional(readOnly=true)
	public List<DtClinicDepartmentEntity> get(String institutionId) {
		// TODO Auto-generated method stub
		return dtClinicDepartmentMapper.selectList(institutionId);
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int saveOrUpdate(DtClinicDepartmentEntity dpt) {
		// TODO Auto-generated method stub
		if (this.get(dpt.getInstitutionId(), dpt.getDptCd()) == null) {
			return dtClinicDepartmentMapper.insert(dpt);
		} else {
			return dtClinicDepartmentMapper.update(dpt);
		}
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public int remove(String institutionId, String dptCd) {
		// TODO Auto-generated method stub
		if (this.containDoctor(institutionId, dptCd)) {
			throw new IllegalDataException("科室中存在医生，无法删除。 institutionId ： " + institutionId + " dptCd：" + dptCd);
		}
		return dtClinicDepartmentMapper.delete(institutionId, dptCd);
	}
	
	@Override
	@Transactional(readOnly=true)
	public boolean containDoctor(String institutionId, String dptCd) {
		// TODO Auto-generated method stub
		return dtClinicDepartmentMapper.selectDptDoctor(institutionId, dptCd);
	}

	@Inject private DtClinicDepartmentMapper dtClinicDepartmentMapper = null;
}
